package cn.jon.androidbanneranimation
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

data class SampleItem(
    val imageRes: Int,
    val title: String,
    val description: String
)

class SampleAdapter(private val items: List<SampleItem>) :
    RecyclerView.Adapter<SampleAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_sample, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        holder.bind(item)
    }

    override fun getItemCount(): Int = items.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val imageView: ImageView = itemView.findViewById(R.id.imageView)
        private val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
        private val descriptionTextView: TextView = itemView.findViewById(R.id.descriptionTextView)

        fun bind(item: SampleItem) {
            imageView.setImageResource(item.imageRes)
            titleTextView.text = item.title
            descriptionTextView.text = item.description

            // 重置动画状态
            imageView.alpha = 1f
            imageView.translationX = 0f
            titleTextView.alpha = 1f
            descriptionTextView.alpha = 1f
        }
    }
}